<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="调研周期:">
        <el-select v-model="queryParams.yearTime" @change="selectChanged" placeholder="请选择年份" style="width:250px;">
          <el-option
            v-for="dict in yearTimeOptions"
            :key="dict.year"
            :label="dict.year"
            :value="dict.year"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label=" " label-width="0px">
        <el-select v-model="queryParams.quarterId" placeholder="请先选择年份" style="width:250px;">
          <el-option
            v-for="dict in quarterTimeOptions"
            :key="dict.id"
            :label="dict.quarterTime"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称:" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border stripe :data="choiceList">
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="副门店代码" align="center" prop="storeA8" />
      <el-table-column label="年份" align="center" prop="yearTime" />
      <el-table-column label="季度" align="center" prop="quarterTime" />
      <el-table-column label="业绩系数得分" align="center" prop="achieveModulus" />
      <el-table-column label="评价自身得分" align="center" prop="evaluateOwnScore" />
      <el-table-column label="评价市场得分" align="center" prop="evaluateMarketScore" />
      <el-table-column label="匹配度值" align="center" prop="matchingNumber" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listChoice, getYear, exportChoice } from "@/api/xdstore/choice";

export default {
  name: "Choice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 巡店评价调研项目列表格数据
      choiceList: [],
      yearData: [], // 年份季度
      yearTimeOptions: [], // 年份列表
      quarterTimeOptions: [], // 季度列表
      // 查询参数
      queryParams: {
        yearTime: undefined,
        quarterId: undefined,
        storeName: undefined,
        pageNum: 1,
        pageSize: 10,
      }
    };
  },
  created() {
    const that = this;
    that.getList();
    that.getYearData(function(data) {
      that.yearTimeOptions = [];
      if (data) {
        for (const year in data) {
          that.yearTimeOptions.push({year: year});
        }
      }
    });
  },
  methods: {
    selectChanged(year) {
      console.log('选择了：'+year);
      this.queryParams.yearTime = year;
      this.quarterTimeOptions = this.yearData[year];
      this.queryParams.quarterId = this.quarterTimeOptions[0].id;
    },
    getYearData(callback) {
      const that = this;
      getYear({}).then(response => {
        that.yearData = response.data || [];
        if (callback) {
          callback(response.data);
        }
      });
    },
    /** 查询加盟店调研评价结果 */
    getList() {
      this.loading = true;
      listChoice(this.queryParams).then(response => {
        this.choiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.yearTime = undefined;
      this.queryParams.quarterId = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有加盟店调研评价结果?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportChoice(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
